import { Material } from './../material-in-item/material-in-item.component';
import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message'
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

export interface MaterialIn {
  id?: string;
  inNum: string;
  totalPrice: number;
  materials: Material[];
}

@Component({
  selector: 'app-form-test',
  templateUrl: './form-test.component.html',
  styleUrls: ['./form-test.component.css']
})
export class FormTestComponent implements OnInit {

  materailIn: MaterialIn = null;

  form: FormGroup;
  constructor(private fb: FormBuilder, private msg: NzMessageService) {
    // 默认是新增数据，为入库单赋初值
    this.materailIn = {
      inNum: '1',
      totalPrice: 0,
      materials: []
    };
    const { inNum, totalPrice, materials } = this.materailIn;
    this.form = this.fb.group({
      'inNum': [inNum, Validators.required],
      'totalPrice': [totalPrice],
      'materials': [materials]
    });
  }

  ngOnInit(): void {

  }

  setTotalPrice(totalPrice) {
    this.form.get('totalPrice').patchValue(totalPrice);
  }

  onSubmit({value, valid}, e: Event): void {
    e.preventDefault();
    if (!valid) {
      this.msg.error('表单验证未通过！');
      return ;
    }
    this.msg.info('表单验证通过！表单信息为' + JSON.stringify(value));
  }

}
